'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _input = require('antd/lib/input');

var _input2 = _interopRequireDefault(_input);

var _reactLifecyclesCompat = require('react-lifecycles-compat');

var _utils = require('../../utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var Group = _input2['default'].Group;

var InputGroup = function (_React$Component) {
  (0, _inherits3['default'])(InputGroup, _React$Component);
  (0, _createClass3['default'])(InputGroup, null, [{
    key: 'getDerivedStateFromProps',
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps,
          getValues = _ref.getValues;

      var nextState = {
        prevProps: props
      };
      if (prevProps && prevProps.children !== props.children) {
        var values = getValues(props);
        nextState.values = values;
      }
      return nextState;
    }
  }]);

  function InputGroup(props) {
    (0, _classCallCheck3['default'])(this, InputGroup);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (InputGroup.__proto__ || Object.getPrototypeOf(InputGroup)).call(this, props));

    _initialiseProps.call(_this);

    var values = _this.getValues(props);
    _this.state = {
      values: values,
      getValues: _this.getValues // eslint-disable-line
    };
    return _this;
  }

  (0, _createClass3['default'])(InputGroup, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var props = (0, _objectWithoutProperties3['default'])(this.props, []);

      ['onChange'].forEach(function (key) {
        return delete props[key];
      });
      var children = (0, _utils.toArrayChildren)(props.children).map(function (item) {
        if (!item) {
          return null;
        }
        var onChange = function onChange(e, isDrag) {
          _this2.onChange(item.key, e, isDrag);
        };
        return _react2['default'].cloneElement(item, (0, _extends3['default'])({}, item.props, { onChange: onChange }));
      });
      return _react2['default'].createElement(Group, props, children);
    }
  }]);
  return InputGroup;
}(_react2['default'].Component);

InputGroup.propTypes = {
  children: _propTypes2['default'].any,
  onChange: _propTypes2['default'].func
};
InputGroup.defaultProps = {
  onChange: function onChange() {}
};

var _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.onChange = function (key, e, isDrag) {
    var values = _this3.state.values;
    var value = e && e.target ? e.target.value : e;
    if (key === 'center') {
      _this3.props.onChange(value);
      Object.keys(values).forEach(function (cKey) {
        values[cKey] = value;
      });
    } else {
      values[key] = value;
      values.center = null;
      _this3.props.onChange(values, isDrag);
    }
    _this3.setState({
      values: values
    });
  };

  this.getValues = function (props) {
    var values = {};
    (0, _utils.toArrayChildren)(props.children).forEach(function (item) {
      var key = item.key;
      values[key] = item.props.defaultValue || item.props.value || item.props.color;
    });
    return values;
  };
};

exports['default'] = (0, _reactLifecyclesCompat.polyfill)(InputGroup);
module.exports = exports['default'];